package com.veryedu.hibernate.hbm.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.veryedu.hibernate.hbm.dao.BaseDao;
import com.veryedu.hibernate.hbm.util.HibernateSessionFactory;

public class BaseDaoImpl implements BaseDao{
	protected Session session;
	protected Transaction tx;
	
	@Override
	public Session getSession() {
		this.session=HibernateSessionFactory.getSession();
		return session;
	}

	@Override
	public void closeSession() {
		if(session!=null){
			session.close();
		}
	}

	@Override
	public boolean add(Object entity) {
		try{
			getSession();
			this.tx=session.beginTransaction();
			session.save(entity);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			closeSession();
		}
		return true;
	}

	@Override
	public boolean delete(Object entity) {
		try{
			getSession();
			this.tx=session.beginTransaction();
			session.delete(entity);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			closeSession();
		}
		return true;
	}

	@Override
	public boolean delete(Class clazz, Integer id) {
		try{
			getSession();
			this.tx=session.beginTransaction();
			Object entity=this.getById(clazz, id);
			session.delete(entity);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			closeSession();
		}
		return true;
	}

	@Override
	public boolean update(Object entity) {
		try{
			getSession();
			this.tx=session.beginTransaction();
			session.update(entity);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			closeSession();
		}
		return true;
	}

	@Override
	public Object getById(Class clazz, Integer id) {
		Object obj=null;
		try{
			getSession();
			this.tx=session.beginTransaction();
			obj=session.get(clazz, id);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			closeSession();
		}
		return obj;
	}

	@Override
	public List findAll(Class clazz) {
		List list=null;
		try{
			getSession();
			this.tx=session.beginTransaction();
			list=session.createQuery("from "+clazz.getName()).list();
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			closeSession();
		}
		return list;
	}

	@Override
	public List findByProperty(Class clazz, String property, Object value) {
		List list=null;
		try{
			getSession();
			this.tx=session.beginTransaction();
			Query query=session.createQuery("from "+clazz.getName()+" as model where model."+property+"=: ?");
			query.setProperties(value);
			list=query.list();
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			closeSession();
		}
		return list;
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void end() {
		// TODO Auto-generated method stub
		
	}

}
